package com.heimali.sf.wraper;

import java.net.URI;

import android.os.AsyncTask;

import com.heimali.sf.config.Constants;
import com.heimali.sf.http.Request;
import com.heimali.sf.utils.NetUtils;

public abstract class BaseWraper {
	//private static final String TAG = "ArticleListWraper";
	protected URI uri;
	protected Callback callback;
	
	protected BaseWraper(){}
	
	protected BaseWraper(URI uri, Callback callback) {
		this.uri = uri;
		this.callback = callback;
	}
	
	protected abstract void onExecute(String result);
	
	public interface Callback{
		public void onSuccess(Object result);
		public void onFail(int errno, String errmsg);
	}
	
	public void execute(){
		if (!NetUtils.isNetAvailable()) {
			callback.onFail(Constants.E_NETWORK_NOT_AVAILABLE, null);
			return ;
		}
		new BaseWraperTask().execute(uri);
	};
	
	class BaseWraperTask extends AsyncTask<Object, Integer, String> {
		@Override
		protected void onPreExecute() {
		}
		
		@Override
		protected String doInBackground(Object... params) {
			return Request.get((URI)params[0]);
		}
		
		@Override
		protected void onPostExecute(String result) {
			onExecute(result);
		}
		
	}
}
